package controller.content;

import controller.entity.Combobox;
import controller.entity.Page;
import controller.entity.Result;
import controller.entity.Tables;
import entity.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import service.GoodsService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by asus on 2017/6/19.
 */
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/list")
    @ResponseBody
    public Tables index(@RequestParam(value="page",required = false) String page,
                             @RequestParam(value="rows", required=false) String rows,
                             @RequestParam(value="title", required=false) String title){
        Integer pageNum=1;
        Integer rowsNum=20;//默认每行记录数
        try{
            pageNum=Integer.parseInt(page);
        }catch (Exception e){}
        try {
            rowsNum=Integer.parseInt(rows);
        }catch (Exception e){}
        Page pageBean=new Page(pageNum,rowsNum,1);
        Tables tables=new Tables();
        tables.setTotal(goodsService.getCount());
        tables.setRows(goodsService.getGoods(pageBean));
        tables.setTitles(title);
        tables.setMethodName("getAllGoods");
        System.out.println("pageBean:"+pageBean);
        System.out.println("tables:"+tables);
        return tables;
    }


    @RequestMapping(value="/add")
    @ResponseBody
    public Result add(Goods goods){
        System.out.println(goods);
        Long id=0L;
        if(null!=goods.getName()&&!"".equals(goods.getName())){
            id=goodsService.saveGoods(goods);
        }
        if(null!=id&&0L!=id){
            return Result.success(id);
        }
        return Result.fail("添加商品失败!!");
    }

    @RequestMapping(value="/list/{id}")
    @ResponseBody
    public Result update(@RequestParam(value="id",required = true) Integer id,Goods goods){
        //TODO 商品各个属性的非空验证
       try{
           goodsService.update(goods);
           return Result.success();
       }catch (Exception e){
           return  Result.fail("商品修改失败");
       }
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(@RequestParam(value="id",required = true) String id){
        //TODO 验证id是否合法
        Long idL;
        try{
           idL= Long.parseLong(id);
        }catch (Exception e){
            return Result.fail("商品删除失败");
        }
        try{
            goodsService.delete(idL);
            return Result.success();
        }catch (Exception e){
            return Result.fail("商品删除失败");
        }
    }

    /**
     * 返回给combobox所需商品信息格式
     * @return
     */
    @RequestMapping("/simplelist")
    @ResponseBody
    public List<Combobox> simpleList(){
        List<Goods> goodsList=goodsService.getAllGoods();
        Combobox comboBox;
        List<Combobox> comboBoxes=new ArrayList<>();
        for(Goods goods:goodsList){
            comboBox=new Combobox();
            comboBox.setId(goods.getId());
            comboBox.setText(goods.getName());
            comboBoxes.add(comboBox);
        }
        return comboBoxes;
    }
}
